package com.zoro.barn.cloud.gateway.security;


import com.zoro.barn.cloud.gateway.loadbalance.WebClientComponent;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;

import java.util.List;

/**
 * 登陆认证过滤器
 *
 * @author zhaoxingwu
 */
public class AuthenticationGatewayFilterFactory extends AbstractGatewayFilterFactory<AuthenticationGatewayFilterFactory.Config> {

    private final static String FILTER_NAME = "AuthenticationGatewayFilter";
    private final WebClientComponent webClientComponent;

    public AuthenticationGatewayFilterFactory(WebClientComponent webClientComponent) {
        /*
        这里调用super是必须的，否则会报类似下面的错误： java.lang.ClassCastException: class java.lang.Object cannot be cast to class com
         .myapp.config.gateway.AuthorizationHeaderFilter$Config (java.lang.Object is in module java.base of loader
         'bootstrap'; com.zoro.barn.cloud.gateway.security.AuthenticationGatewayFilterFactory$Config is in unnamed
         module of loader 'app')
            at com.zoro.barn.cloud.gateway.security.AuthenticationGatewayFilterFactory.apply
            (AuthenticationGatewayFilterFactory.java:27)
            ~[classes/:na]
         */
        super(Config.class);
        this.webClientComponent = webClientComponent;
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new AuthenticationGatewayFilter(config.ignoreUriPattern, config.ignoreUri, this.webClientComponent, config.checkTokenUrl);
    }

    @Override
    public String name() {
        return FILTER_NAME;
    }

    public static class Config {

        private List<String> ignoreUriPattern;
        private List<String> ignoreUri;
        private String checkTokenUrl = "/login/ps/valid";

        public String getCheckTokenUrl() {
            return checkTokenUrl;
        }

        public void setCheckTokenUrl(String checkTokenUrl) {
            this.checkTokenUrl = checkTokenUrl;
        }

        public List<String> getIgnoreUriPattern() {
            return ignoreUriPattern;
        }

        public void setIgnoreUriPattern(List<String> ignoreUriPattern) {

            this.ignoreUriPattern = ignoreUriPattern;
        }

        public List<String> getIgnoreUri() {
            return ignoreUri;
        }

        public void setIgnoreUri(List<String> ignoreUri) {
            this.ignoreUri = ignoreUri;
        }
    }
}
